package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcOrigsectionEntity;
import com.dhcc.cdc.busi.service.ICdcOrigsectionService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 文档节
 * 
 * @ClassName: CdcOrigsectionController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcorigsection")
public class CdcOrigsectionController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcorigsection/";

	@Autowired
	private ICdcOrigsectionService cdcOrigsectionService;
	

    /**
     * 跳转到文档节列表首页
     */
    @RequestMapping("")
    @Permission("cdcorigsection:list")
    public String index() {
        return PREFIX + "cdcorigsection.html";
    }
	
    /**
     * 跳转到新增文档节
     */
    @RequestMapping("/to_add")
    @Permission("cdcorigsection:add")
    public String toAdd() {
        return PREFIX + "cdcorigsection_add.html";
    }
	
	/**
     * 跳转到修改文档节
     */
    @RequestMapping("/to_update/{cdcOrigsectionId}")
    @Permission("cdcorigsection:update")
    public String toUpdate(@PathVariable Long cdcOrigsectionId, Model model) {
        CdcOrigsectionEntity cdcOrigsection = this.cdcOrigsectionService.selectById(cdcOrigsectionId);
        model.addAttribute("cdcOrigsection",cdcOrigsection);
        return PREFIX + "cdcorigsection_edit.html";
    }

    /**
     * 获取文档节列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcorigsection:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcOrigsectionEntity> page = new PageFactory<CdcOrigsectionEntity>().defaultPage();
        List<CdcOrigsectionEntity> result = this.cdcOrigsectionService.page(page,params);
        page.setRecords((List<CdcOrigsectionEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增文档节
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcorigsection:add")
    @BussinessLog(value = "新增文档节")
    public Object add(CdcOrigsectionEntity cdcOrigsection) {
        if (CommonUtil.isOneEmpty(cdcOrigsection)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcOrigsection.insert();
        return R.ok();
    }

    /**
     * 删除文档节
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcorigsection:delete")
    @BussinessLog(value = "删除文档节")
    public Object delete(@RequestParam Long cdcOrigsectionId) {
	
        this.cdcOrigsectionService.deleteById(cdcOrigsectionId);

        return R.ok();
    }

    /**
     * 修改文档节
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcorigsection:update")
    @BussinessLog(value = "修改文档节")
    public Object update(CdcOrigsectionEntity cdcOrigsection) {
        if (CommonUtil.isOneEmpty(cdcOrigsection)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcOrigsectionEntity old = this.cdcOrigsectionService.selectById(cdcOrigsection.getId());

        old.setCode(cdcOrigsection.getCode());
        old.setDescription(cdcOrigsection.getDescription());
        old.setChapterId(cdcOrigsection.getChapterId());
        old.setDocTypeId(cdcOrigsection.getDocTypeId());
        old.setContent(cdcOrigsection.getContent());
        old.setIsStructed(cdcOrigsection.getIsStructed());
        old.setIsActive(cdcOrigsection.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
